﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SilverMindMap.Core.Tests.TestHelpers
{
    public static class ExceptionAssert
    {
        public static void Thrown(Action action, Predicate<Exception> exceptionPredicate, string errorMessage)
        {
            Exception thrownException = null;
            try
            {
                action();
            }
            catch (Exception ex)
            {
                thrownException = ex;                
            }

            if (thrownException == null)
            {
                string finalErrorMessage = string.Format("Exception was expected, but non was thrown. Error message: {0}", errorMessage);
                Assert.Fail(errorMessage);
            }
            else if (!exceptionPredicate(thrownException))
            {
                Assert.Fail(errorMessage);
            }
        }
    }
}
